from pages import company_map
from common.mysql_api import JetMySQL
from common.getConfig import logger
from common.generate_excel_file import GenerateExcelFile


def spider(sql, info_dic):
    logger.info("Task:%s..is staring" % info_dic["category"])
    spider_instance = company_map[info_dic["companyName"]]
    spider = spider_instance(info_dic["url"])
    for info in spider.get_all_items(info_dic.get("format", 1)):
        info.update(info_dic)
        sql.insert(info)


if __name__ == "__main__":
    info_dic = dict()
    sql = JetMySQL()
    # company = "Joann"
    # category = "spring-decor"
    # url = "https://www.joann.com/home-decor-and-holiday/holidays-seasons/spring/spring-decor/?prefn1=ecommerceType2&prefv1=Garden%20Decor%20%26%20Statues&prefn2=productCategory&prefv2=Spring%20Gardening&srule=best-sellers"

    company = "Amazon"
    category = "national_tree_garden"
    url = "https://www.amazon.com/s?k=national+tree&i=garden&rh=n%3A1063278%2Cp_89%3ABalsam+Hill%7CNational+Tree+Company&dc&qid=1617526488&rnid=2528832011&ref=sr_nr_p_89_2"
    format = 2

    info_dic["companyName"] = company
    info_dic["category"] = category
    info_dic["url"] = url
    # info_dic["format"] = format
    sql.insert(info_dic, "product")

    spider(sql, info_dic)
    excel = GenerateExcelFile()
    excel.excel_main(sql, company, category)
